using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Domain.Entities;
using System.Collections.Generic;
using Domain.Services;
using Domain.Misc;
using UserInterface.Misc;

namespace UserInterface.PM
{
    public partial class Archive : BoardPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Menu.IsArchiveActive = true;
            
            if (!IsPostBack)
            {
                ShowMessages();
            }
        }

        private void ShowMessages()
        {
            User user = CurrentSession.User;
            int messageCount = PrivateMessageService.GetArchiveCount(user);
            int messagesPerPage = PagingTools.GetMessagesPerPage(user);
            int pageCount = PagingTools.GetPageCount(messageCount, messagesPerPage);
            int page = PagingTools.GetPage(Request.QueryString["page"], pageCount);

            IList<PrivateMessage> messages = PrivateMessageService.GetArchive(user, page, messagesPerPage);

            MessageRepeater.DataSource = messages;
            MessageRepeater.DataBind();

            if (pageCount < 2)
            {
                TopPager.Visible = BottomPager.Visible = false;
            }
            else
            {
                TopPager.CurrentPage = BottomPager.CurrentPage = page;
                TopPager.PageCount = BottomPager.PageCount = pageCount;
                TopPager.UrlBase = BottomPager.UrlBase = "/PM/Archive.aspx";
            }

            if (messageCount == 0)
            {
                ArchiveInfo.Text = (string)GetLocalResourceObject("NoMessagesWarning");
            }
            else
            {
                ArchiveInfo.Text = String.Format(
                    (string)GetLocalResourceObject("ArchiveInfo"),
                    messageCount,
                    pageCount
                );
            }

            string pageTitle = GetLocalResourceObject("Title") as string;

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(pageTitle, "/PM/Archive.aspx");
        }

        protected void MessageRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int messageId = int.Parse((string)e.CommandArgument);
            PrivateMessage message = PrivateMessageService.GetById(messageId);
            if (message != null)
            {
                if (e.CommandName == "Delete")
                {
                    PrivateMessageService.Trash(CurrentSession.User, message);
                }
                else if (e.CommandName == "Read")
                {
                    Server.Transfer("~/PM/Read.aspx?messageId=" + message.Id + "&ref=archive");
                }
                else if (e.CommandName == "MarkRead")
                {
                    PrivateMessageService.MarkRead(message);
                }
                else if (e.CommandName == "MarkUnread")
                {
                    PrivateMessageService.MarkUnread(message);
                }
                ShowMessages();
            }
        }

        protected void MessageRepeater_ItemDataBound(object source, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Button deleteButton = (Button)e.Item.FindControl("DeleteButton");
                string confirmMessage = (string)GetLocalResourceObject("ConfirmDeleteMessage");
                UITools.AddConfirmMessage(deleteButton, confirmMessage);

                PrivateMessage message = (PrivateMessage)e.Item.DataItem;

                Button markReadButton = (Button)e.Item.FindControl("MarkReadButton");
                Button markUnreadButton = (Button)e.Item.FindControl("MarkUnreadButton");

                markReadButton.Visible = !message.IsRead;
                markUnreadButton.Visible = message.IsRead;
            }
        }

        protected string GetRowClass(object dataItem)
        {
            PrivateMessage message = dataItem as PrivateMessage;
            return message.IsRead ? "read" : "unread";
        }
    }
}
